package com.yuhanginfo.modules.web.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.WebCrudService;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.dao.WebInvoiceDao;
import com.yuhanginfo.modules.web.entity.WebInvoice;
import com.yuhanginfo.modules.web.entity.WebYongUser;

/**
 * 开票管理Service
 * @author lvls
 * @version 2017-10-30
 */
@Service
@Transactional(readOnly = true)
public class WebInvoiceService extends WebCrudService<WebInvoiceDao, WebInvoice> {

	@Autowired
	private WebInvoiceDao webInvoiceDao;
	public WebInvoice get(String id) {
		return super.get(id);
	}
	
	public List<WebInvoice> findList(WebInvoice invoice) {
		return super.findList(invoice);
	}
	
	public Page<WebInvoice> findPage(Page<WebInvoice> page, WebInvoice invoice) {
		return super.findPage(page, invoice);
	}
	
	@Transactional(readOnly = false)
	public void save(WebInvoice invoice) {
		super.save(invoice);
	}
	
	@Transactional(readOnly = false)
	public void delete(WebInvoice invoice) {
		if(!this.validate(invoice.getId(), WebUserUtils.getYongUser().getId())){
			return;
		}
		super.delete(invoice);
	}

	/**
	 * 根据userId查询出默认的开票
	 */
	public WebInvoice getDefInvoice(String userId) {
		return webInvoiceDao.getDefInvoice(userId);
	}
	
	/**
	 * 根据userId查询出所有的开票
	 */
	public List<WebInvoice> selectInvoiceByUserId(WebInvoice invoice) {
		return webInvoiceDao.selectInvoiceByUserId(invoice);
	}
	
	//新增开票信息
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public void saveWebInvoice(WebInvoice invoice){
		Date now = new Date();
		WebYongUser user = WebUserUtils.getYongUser();
		//如果勾选了默认则将所有变为非默认
		if("1".equals(invoice.getIsDef())){
			WebInvoice updateIsDef =  new WebInvoice();
			updateIsDef.setIsDef("0");
			updateIsDef.setUserId(WebUserUtils.getYongUser().getId());
			webInvoiceDao.updateIsDefByUserId(updateIsDef);
		}else{
			invoice.setIsDef("0");
		}
		if(null != invoice.getId() && !"".equals(invoice.getId())){
			boolean result = this.validate(invoice.getId(), user.getId());
			if(!result){//如果当前传入的ID不属于当前用户，则不进行更新
				return;
			}
			invoice.setUpdateDate(now);
			webInvoiceDao.update(invoice);
		}else{
			invoice.setCreateBy(user);
			invoice.setCreateDate(now);
			invoice.setUpdateBy(user);
			invoice.setUpdateDate(now);
			invoice.setUserId(user.getId());
			webInvoiceDao.insert(invoice);
		}
		
	}
	
	//设置默认
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public void setDef(WebInvoice invoice){
		if(!this.validate(invoice.getId(), WebUserUtils.getYongUser().getId())){
			return;
		}
		//将用户下所有发票设置为非默认
		invoice.setIsDef("0");
		invoice.setUserId(WebUserUtils.getYongUser().getId());
		webInvoiceDao.updateIsDefByUserId(invoice);
		//根据ID设置为默认
		invoice.setIsDef("1");
		webInvoiceDao.updateIsDefById(invoice);
	}
	
	//验证数据是否属于用户
	private boolean validate(String id,String userId){
		//验证数据是否属于当前用户
		WebInvoice validate = new WebInvoice();
		validate.setId(id);
		validate.setUserId(userId);
		int count = webInvoiceDao.getValidate(validate);
		if(count == 0){
			return false;
		}
		return true;
	}
}